package com.xmut.bs.handaccount.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.xmut.bs.handaccount.GlideApp;
import com.xmut.bs.handaccount.R;
import com.xmut.bs.handaccount.activity.AddGoodsActivity;
import com.xmut.bs.handaccount.util.StringUtil;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.photopicker.activity.BGAPhotoPickerActivity;

public class AddImgAdapter extends CommonAdapter<String> {

    private List<String> mList;

    public AddImgAdapter(Context context, List<String> datas) {
        super(context, R.layout.item_img, datas);
        mList = new ArrayList<>();
    }

    public void setData(List<String> data) {
        mDatas.clear();
        mDatas.addAll(data);
        notifyDataSetChanged();
    }

    public void addItem(String data) {
        mDatas.add(mDatas.size() > 0 ? mDatas.size() - 1 : 0, data);
        if (mDatas.size() > 9) {
            mDatas.remove(mDatas.size() - 1);
        }
        notifyDataSetChanged();
    }

    public void removeLastItem() {
        mDatas.remove(mDatas.size() - 2);
        notifyDataSetChanged();
    }

    public void addUrl(String data) {
        mList.add(data);
    }

    public String getUrl() {
        String s = "";
        for (String ss : mList) {
            s = s.concat(ss).concat(",");
        }
        if(!StringUtil.isNull(s)){
            s = s.substring(0, s.length() - 1);
        }
        return s;
    }

    @Override
    protected void convert(ViewHolder holder, String s, final int position) {
        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position == mDatas.size() - 1 && mDatas.get(position).equals("")) {
                    File file = new File("temp.jpg");
                    ((AddGoodsActivity) mContext).startActivityForResult(BGAPhotoPickerActivity.newIntent(mContext, file, 1,
                            null, false), AddGoodsActivity.REQUEST_CODE_CHOOSE);
                }
            }
        });
        holder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (!mDatas.get(position).equals("")) {
                    mList.remove(position);
                    mDatas.remove(position);
                    if (mDatas.size() == 8) {
                        mDatas.add("");
                    }
                    notifyDataSetChanged();
                }
                return false;
            }
        });
        ImageView view = holder.getView(R.id.img);
        if (position == mDatas.size() - 1 && mDatas.get(position).equals("")) {
            view.setImageResource(R.mipmap.bga_pp_ic_plus);
        } else {
            GlideApp.with(view)
                    .load(s)
                    .placeholder(R.color.grey300)
                    .centerCrop()
                    .error(R.color.grey300)
                    .into(view);
        }
    }
}